local juqi = fk.CreateSkill{
  name = "hanqing__juqi",
}

Fk:loadTranslationTable{
  ["hanqing__juqi"] = "居奇",
  [":hanqing__juqi"] = "轮次开始时或准备阶段，你可以明置你与一名角色各一张牌。你可以将明置手牌当【无中生有】使用。"..
  "有明置牌的角色每轮首次造成伤害后，你可以与其各摸一张牌。",

  ["#hanqing__juqi"] = "居奇：你可以将明置手牌当【无中生有】使用",
  ["#hanqing__juqi-choose"] = "居奇：你可以明置你与一名角色各一张牌",
  ["#hanqing__juqi-show"] = "居奇：明置 %dest 的%arg张手牌",
  ["#hanqing__juqi-invoke"] = "居奇：是否与 %dest 各摸一张牌？",
}

local DIY = require "packages/diy_utility/diy_utility"

juqi:addEffect("viewas", {
  anim_type = "drawcard",
  pattern = "ex_nihilo",
  prompt = "#hanqing__juqi",
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and table.contains(DIY.getShownCards(player), to_select)
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 then return end
    local card = Fk:cloneCard("ex_nihilo")
    card:addSubcards(cards)
    card.skillName = juqi.name
    return card
  end,
  enabled_at_response = function (self, player, response)
    return not response and #DIY.getShownCards(player) > 0
  end,
})

local spec = {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(juqi.name) and
      (event == fk.EventPhaseStart and target == player and player.phase == Player.Start or event == fk.RoundStart) and
      player:getHandcardNum() - #DIY.getShownCards(player) > 0
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room.alive_players, function (p)
      return p:getHandcardNum() - #DIY.getShownCards(p) > 0
    end)
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = targets,
      skill_name = juqi.name,
      prompt = "#hanqing__juqi-choose",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local cards = table.filter(player:getCardIds("h"), function (id)
      return not table.contains(DIY.getShownCards(player), id)
    end)
    local n = to == player and math.min(2, #cards) or 1
    cards = room:askToCards(player, {
      min_num = n,
      max_num = n,
      include_equip = false,
      skill_name = juqi.name,
      pattern = tostring(Exppattern{ id = cards }),
      prompt = "#hanqing__juqi-show::"..player.id..":"..n,
      cancelable = false,
    })
    DIY.showCards(player, cards)
    if to ~= player and not player.dead and not to.dead then
      cards = table.filter(to:getCardIds("h"), function (id)
        return not table.contains(DIY.getShownCards(to), id)
      end)
      if #cards > 0 then
        local card_data, extra_data, visible_data = {}, {}, {}
        table.insert(card_data, {"$Hand", cards})
        for _, id in ipairs(cards) do
          if not player:cardVisible(id) then
            visible_data[tostring(id)] = false
          end
        end
        if next(visible_data) == nil then visible_data = nil end
        extra_data.visible_data = visible_data
        extra_data.min = 1
        extra_data.max = 1
        extra_data.prompt = "#hanqing__juqi-show::"..to.id..":1"
        local result = room:askToPoxi(player, {
          poxi_type = "AskForCardsChosen",
          data = card_data,
          extra_data = extra_data,
          cancelable = false,
        })
        DIY.showCards(to, result)
      end
    end
  end,
}
juqi:addEffect(fk.EventPhaseStart, spec)
juqi:addEffect(fk.RoundStart, spec)

juqi:addEffect(fk.Damage, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(juqi.name) and target and
      #DIY.getShownCards(target) > 0 and not target.dead then
      local damage_events = player.room.logic:getActualDamageEvents(1, function (e)
        return e.data.from == target
      end, Player.HistoryRound)
      return #damage_events == 1 and damage_events[1].data == data
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    if room:askToSkillInvoke(player, {
      skill_name = juqi.name,
      prompt = "#hanqing__juqi-invoke::"..target.id,
    }) then
      event:setCostData(self, {tos = {target}})
      return true
    end
  end,
  on_use = function (self, event, target, player, data)
    player:drawCards(1, juqi.name)
    if not target.dead then
      target:drawCards(1, juqi.name)
    end
  end,
})

juqi:addAI(nil, "vs_skill")

return juqi
